package com.springdoor.goods.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.springdoor.common.constant.UserConstants;
import com.springdoor.common.core.domain.TreeSelect;
import com.springdoor.common.utils.SecurityUtils;
import com.springdoor.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springdoor.goods.mapper.SprdGoodsBrandMapper;
import com.springdoor.common.core.domain.entity.SprdGoodsBrand;
import com.springdoor.goods.service.ISprdGoodsBrandService;

/**
 * 商品品牌Service业务层处理
 * 
 * @author llshs
 * @date 2024-04-30
 */
@Service
public class SprdGoodsBrandServiceImpl implements ISprdGoodsBrandService 
{
    @Autowired
    private SprdGoodsBrandMapper brandMapper;

    /**
     * 查询商品品牌
     * 
     * @param brandId 商品品牌主键
     * @return 商品品牌
     */
    @Override
    public SprdGoodsBrand selectSprdGoodsBrandByBrandId(Long brandId)
    {
        return brandMapper.selectSprdGoodsBrandByBrandId(brandId);
    }

    /**
     * 查询商品品牌列表
     * 
     * @param brand 商品品牌
     * @return 商品品牌
     */
    @Override
    public List<SprdGoodsBrand> selectSprdGoodsBrandList(SprdGoodsBrand brand)
    {
        return brandMapper.selectSprdGoodsBrandList(brand);
    }

    /**
     * 新增商品品牌
     * 
     * @param brand 商品品牌
     * @return 结果
     */
    @Override
    public int insertSprdGoodsBrand(SprdGoodsBrand brand)
    {
        return brandMapper.insertSprdGoodsBrand(brand);
    }

    /**
     * 修改商品品牌
     * 
     * @param brand 商品品牌
     * @return 结果
     */
    @Override
    public int updateSprdGoodsBrand(SprdGoodsBrand brand)
    {
        if (brand != null) {
            String loginUser = SecurityUtils.getUsername();
            brand.setUpdateBy(loginUser);
        }
        return brandMapper.updateSprdGoodsBrand(brand);
    }

    /**
     * 批量删除商品品牌
     * 
     * @param brandIds 需要删除的商品品牌主键
     * @return 结果
     */
    @Override
    public int deleteSprdGoodsBrandByBrandIds(Long[] brandIds)
    {
        return brandMapper.deleteSprdGoodsBrandByBrandIds(brandIds);
    }

    /**
     * 删除商品品牌信息
     * 
     * @param brandId 商品品牌主键
     * @return 结果
     */
    @Override
    public int deleteSprdGoodsBrandByBrandId(Long brandId)
    {
        return brandMapper.deleteSprdGoodsBrandByBrandId(brandId);
    }

    @Override
    public boolean checkBrandCodeUnique(SprdGoodsBrand brand) {
        Long brandId = StringUtils.isNull(brand.getBrandId()) ? -1L : brand.getBrandId();
        SprdGoodsBrand info = brandMapper.checkBrandCodeUnique(brand);
        if (StringUtils.isNotNull(info) && info.getBrandId().longValue() != brandId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkBrandNameUnique(SprdGoodsBrand brand) {
        Long brandId = StringUtils.isNull(brand.getBrandId()) ? -1L : brand.getBrandId();
        SprdGoodsBrand info = brandMapper.checkBrandNameUnique(brand);
        if (StringUtils.isNotNull(info) && info.getBrandId().longValue() != brandId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<SprdGoodsBrand> selectBrandAll() {
        return brandMapper.selectBrandAll();
    }

    @Override
    public List<TreeSelect> buildAttrTreeSelect(List<SprdGoodsBrand> list) {
        List<SprdGoodsBrand> trees = buildAttrTree(list);
        return trees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private List<SprdGoodsBrand> buildAttrTree(List<SprdGoodsBrand> list) {
        List<SprdGoodsBrand> returnList = new ArrayList<SprdGoodsBrand>();
        List<Long> tempList = list.stream().map(SprdGoodsBrand::getBrandId).collect(Collectors.toList());
        for (Iterator<SprdGoodsBrand> iterator = list.iterator(); iterator.hasNext();)
        {
            SprdGoodsBrand type = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(type.getParentId()))
            {
                recursionFn(list, type);
                returnList.add(type);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = list;
        }
        return returnList;
    }

    private void recursionFn(List<SprdGoodsBrand> list, SprdGoodsBrand b) {
        // 得到子节点列表
        List<SprdGoodsBrand> childList = getChildList(list, b);
        b.setChildren(childList);
        for (SprdGoodsBrand tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    private boolean hasChild(List<SprdGoodsBrand> list, SprdGoodsBrand b) {
        return getChildList(list, b).size() > 0;
    }

    private List<SprdGoodsBrand> getChildList(List<SprdGoodsBrand> list, SprdGoodsBrand b) {
        List<SprdGoodsBrand> tlist = new ArrayList<SprdGoodsBrand>();
        Iterator<SprdGoodsBrand> it = list.iterator();
        while (it.hasNext())
        {
            SprdGoodsBrand n = it.next();
            if (n.getParentId().longValue() == b.getBrandId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
